package com.example.kotlindemo

import org.junit.Test

import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }
    @Test
    fun test1():Unit{
        var a = 1;
        a= 2.3.toInt();

        var name="哈哈哈"

        var dis="aaa"

        var f=dis===name;

        var str="""
            11
            ${f}
            22
        """.trimIndent();


        val array = arrayOf<Int>(1, 2, 3)
        println(array.get(2));


        //可变数组
        val listOf = mutableListOf(2)
        listOf.add(3)

        var mutableMapOf = mutableMapOf<String,Int>()
        mutableMapOf.set("a",1 );


//        var set = mutableMapOf<Int,String>(1 to "2", 2 to "3");
//        println(set[2]);
//        set.put(3,"4");
//        set.remove(2);


        var set = mutableSetOf<String>();
        set.add("a");
        set.remove("e")

        var p=Pair("a", "b");

        var c=Triple("a","b","c");

//        println(str);

        val ins=A();

//        ins.test()


        val f0=1 .. 10;
        val f1=1 until 10;
        val f2=1 ..< 10;
        (1 .. 10 step 2).forEach(::println);
        val r1='a'..'z' step 2;
        for (i in r1){
            println(i)
        }

    }

    @Test
    fun test2(){
        val list=(1 .. 20).toList();
        val iterator = list.listIterator()
        while (iterator.hasNext()){
            println(iterator.next())
        }
        for(i in list){

        }
        //此处i可以解构  (index,value)
        for(i in list.withIndex()){
            println("${i.index} ${i.value}");
        }

        run a@{
            list.forEachIndexed{ index, element ->
                println("Index: $index, Element: $element")
                //跳出循环的写法
                return@a;
            }
        }
    }

    @Test
    fun test3(){
        // 使用 test4 函数对 Person 对象进行配置
        fun <T> test4(func: T.() -> Unit): T {
            val instance = Person() as T // 创建一个实例（此处为简单示例）
            instance.func() // 在实例上调用扩展函数
            return instance
        }

        val person = test4<Person> {
            name = "Alice" // 直接访问 Person 的属性
            age = 25
        }
        test4(person)

        println(person);
    }

    fun test4(p1:Person?) {
        p1?.apply {
            age=10;
            name="aaa"
        }
    }

}



class Person(var page:Int){
    var age:Int;
    var name=""
        get() {
            return "aaa"
        }
        set(value) {
            field=value
        }
    init {
        //初始化逻辑
        age=page
    }
    constructor() : this(2);

    operator fun component1()=name;
    operator fun component2()=age;

    override fun toString(): String {
        return "姓名：${name} 年龄:${age}"
    }
}

class A{
    var a:Int?=null;
    lateinit var b:String;
    fun test(){
        //不为空执行，为空返回Null
        println(a?.toInt());
        //不为空执行，为空抛出异常
        println(a!!.toInt())
        //延迟初始化，编译器不会报错，但是运行时如果为空会崩溃
        println(b.length)
    }

    fun test2(p1:Int){
        //if语句块的最后一行是a的值，多分支也是可以的
        var a=if(p1>3){
            "大于"
        }else{
            "小于"
        }

        a@ for (i in 1..10){
            for (j in 1..10){
                if(j==5){
                    break@a;
                    continue@a;
                }
            }
        }



    }
    @Test
    fun test3(){

        println(3.my(4));

        sum(a=2,b=3,{e->
            println("e"+e);
        });

        var list=(1 .. 4).toList();
        funcList(*list.toIntArray());
    }

    fun sum(a:Int,b:Int=2,c:(e:String)->Unit):Int{
        c(" haha");
        return a+b
    };


    fun funcList(vararg list:Int){

    }

    infix fun Int.my(a:Int)=this+a;
}